<?php
/**
 *
 * Controller.php
 * User: kalvin
 * Date: 2018/1/24
 * Time: 下午5:57
 */

namespace core\lib;


class Controller
{
    public function view($file, Array $data = [])
    {
        $basefileDir = RESOURCE . 'view/';
        $cacheDir =CACHE;

        $extension = '.blade.php';
        //查看文件是否有点，由于文件路径通过点分割
        $res = strpos($file, '.');

        if (!$res) {
            //没有点直接加载
            // $directory=$basefileDir.$file.$extension;
            //定义模板目录
            $file=$file.$extension;
            $loader = new \Twig_Loader_Filesystem($basefileDir);
            $twig = new \Twig_Environment($loader,array(
                'cache'=> $cacheDir,
                'debug'=>config('app.debug'),
                'extension'=>$extension
            ));
            return $twig->render($file,$data);
        } else {
            //有点替换点为'/'作为路径
            $filedir=str_replace('.','/',$file);
            $file=$filedir.$extension;
            $loader = new \Twig_Loader_Filesystem($basefileDir);
            $twig = new \Twig_Environment($loader,array(
                'cache'=> $cacheDir,
                'debug'=>config('app.debug'),
                'extension'=>$extension
            ));
            return $twig->render($file,$data);

        }
    }
}